package com.moke.util.base
{
	public class StringUtilities
	{
		private static var codeMap:Object ;
		private static var reverseCodeMap:Object ;

		public static function encode(original:String):String
		{
			if (codeMap == null)
			{
				codeMap = new Object();
				var originalMap:Array = new Array();
				for (var i:int = 0;i < 256;i++)
				{
					originalMap.push(i);
				}
				var tempChars:Array = originalMap.concat();
				for (var j:int = 0;j < originalMap.length;j++)
				{
					var randomIndex:int = Math.floor(Math.random() * (tempChars.length - 1));
					codeMap[originalMap[j]] = tempChars[randomIndex];
					tempChars.splice(randomIndex,1);
				}
			}

			var characters:Array = original.split("");
			for (var k:uint = 0;k < characters.length;k++)
			{
				characters[k] = String.fromCharCode(codeMap[String(characters[k]).charCodeAt(0)]);
			}
			return characters.join("");
		}

		public static function decode(encoded:String):String
		{
			var characters:Array = encoded.split("");

			if (reverseCodeMap == null)
			{
				reverseCodeMap = new Object();
				for (var key:* in codeMap)
				{
					reverseCodeMap[codeMap[key]] = key;
				}
			}

			for (var i:int = 0;i < characters.length;i++)
			{
				characters[i] = String.fromCharCode(reverseCodeMap[String(characters[i]).charCodeAt(0)]);
			}
			return characters.join("");
		}

		public static function isWhitespace(ch:String):Boolean
		{
			return ch == '\r' || ch == '\n' || ch == '\f' || ch == '\t' || ch == ' ';
		}

		public static function trim(original:String):String
		{			
			var characters:Array = original.split("");
			for (var i:int = 0;i < characters.length;i++)
			{
				if (isWhitespace(characters[i]))
				{
					characters.splice(i,1);
					i--;
				}
				else
				{
					break;
				}
			}

			for (i = characters.length - 1;i >= 0;i--)
			{
				if (isWhitespace(characters[i]))
				{
					characters.splice(i,1);
				}
				else
				{
					break;
				}
			}

			return characters.join("");
		}

		/**　语句首字母大写处理  */
		public static function toTitleCase(original:String):String
		{
			var words:Array = original.split(" ");
			for (var i:int = 0;i < words.length;i++)
			{
				words[i] = toInitialCap(words[i]);
			}
			return (words.join(" "));
		}

		/**　首字母大写处理  */
		public static function toInitialCap(original:String):String
		{
			return original.charAt(0).toUpperCase() + original.substr(1).toLowerCase();
		}

		/** 移除文件扩展名*/
		public static function removeExtension(filename:String):String
		{
			var extensionIndex:Number = filename.lastIndexOf('.');
			if (extensionIndex == -1)
			{
				return filename;
			}
			else
			{
				return filename.substr(0,extensionIndex);
			}
		}

		/** 获取文件扩展名*/
		public static function extractExtension(filename:String):String
		{
			var extensionIndex:Number = filename.lastIndexOf('.');
			if (extensionIndex == -1)
			{
				return "";
			}
			else
			{
				return filename.substr(extensionIndex + 1,filename.length);
			}
		}

	}
}
